package com.cup.newSecurity.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import com.cup.common.logging.Logger;
import com.cup.newSecurity.constant.LdapConstants;
import com.cup.newSecurity.exception.LdapBaseException;
import com.cup.newSecurity.util.LdapConnectionFactory;
import com.cup.newSecurity.util.LdapServer;
import com.cup.newSecurity.vo.BaseUserInfo;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPModification;
import com.novell.ldap.connectionpool.PoolManager;

public class AttributeServiceImpl {
	static Logger logger = Logger.getLogger(AttributeServiceImpl.class);

	private PoolManager ldPool;

	public AttributeServiceImpl() throws LdapBaseException {
		try {
			ldPool = LdapConnectionFactory.getInstance().getLdPool();
		} catch (LdapBaseException e) {
			String message = "CAN NOT CONNECT TO LDAP SERVER.";
			logger.error(message, e);
			throw new LdapBaseException(message, e);
		}
	}

	public List<String> search(BaseUserInfo user, String attribute)
			throws Exception {
		List<String> result = new ArrayList<String>();
		String userEntryDn = LdapConstants.UID + LdapConstants.EQUAL
				+ user.getUserID() + LdapConstants.FLAG + LdapConstants.OU
				+ LdapConstants.EQUAL + user.getInstID() + LdapConstants.FLAG
				+ LdapConstants.INSTITUTE + LdapConstants.FLAG
				+ LdapConstants.SEARCHBAKSE;
		PoolManager pm = LdapConnectionFactory.getInstance().getLdPool();
		LDAPConnection ld = pm.getBoundConnection(LdapServer.getInstance()
				.getManager(), LdapServer.getInstance().getManagerPass()
				.getBytes());
		LDAPEntry entry = ld.read(userEntryDn, new String[] { attribute });
		if (entry.getAttribute(attribute) != null) {
			for (String s : entry.getAttribute(attribute).getStringValueArray()) {
				result.add(s);
			}
		}
		return result;
	}

	public void add(BaseUserInfo user, String attribute, String value)
			throws Exception {
		String userEntryDn = LdapConstants.UID + LdapConstants.EQUAL
				+ user.getUserID() + LdapConstants.FLAG + LdapConstants.OU
				+ LdapConstants.EQUAL + user.getInstID() + LdapConstants.FLAG
				+ LdapConstants.INSTITUTE + LdapConstants.FLAG
				+ LdapConstants.SEARCHBAKSE;
		PoolManager pm = LdapConnectionFactory.getInstance().getLdPool();
		LDAPConnection ld = pm.getBoundConnection(LdapServer.getInstance()
				.getManager(), LdapServer.getInstance().getManagerPass()
				.getBytes());
		List<String> result = new ArrayList<String>();
		LDAPEntry entry = ld.read(userEntryDn, new String[] { attribute });
		if (entry.getAttribute(attribute) != null) {
			for (String s : entry.getAttribute(attribute).getStringValueArray()) {
				result.add(s);
			}
		}
		LDAPModification delete = null;
		if (!result.isEmpty()) {
			LDAPAttribute attr = new LDAPAttribute(attribute);
			delete = new LDAPModification(LDAPModification.DELETE, attr);
		}
		result.add(value);
		
		LDAPModification[] modifications = new LDAPModification[delete == null ? result.size() : result.size() + 1];
		int i = 0;
		if (delete != null) {
			modifications[i++] = delete;
		}
		for (; i < modifications.length; i++) {
			modifications[i] = new LDAPModification(LDAPModification.ADD, new LDAPAttribute(attribute, result.get(delete == null ? i : i - 1)));
		}
		ld.modify(userEntryDn, modifications);
	}
}
